home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / atom / token_store.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.2 KB  |  112 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''This module provides a TokenStore class which is designed to manage
  5. auth tokens required for different services.
  6.  
  7. Each token is valid for a set of scopes which is the start of a URL. An HTTP
  8. client will use a token store to find a valid Authorization header to send
  9. in requests to the specified URL. If the HTTP client determines that a token
  10. has expired or been revoked, it can remove the token from the store so that
  11. it will not be used in future requests.
  12. '''
  13. __author__ = 'api.jscudder (Jeff Scudder)'
  14. import atom.http_interface as atom
  15. import atom.url as atom
  16. SCOPE_ALL = 'http'
  17.  
  18. class TokenStore(object):
  19.     '''Manages Authorization tokens which will be sent in HTTP headers.'''
  20.     
  21.     def __init__(self, scoped_tokens = None):
  22.         if not scoped_tokens:
  23.             pass
  24.         self._tokens = { }
  25.  
  26.     
  27.     def add_token(self, token):
  28.         '''Adds a new token to the store (replaces tokens with the same scope).
  29.  
  30.     Args:
  31.       token: A subclass of http_interface.GenericToken. The token object is 
  32.           responsible for adding the Authorization header to the HTTP request.
  33.           The scopes defined in the token are used to determine if the token
  34.           is valid for a requested scope when find_token is called.
  35.  
  36.     Returns:
  37.       True if the token was added, False if the token was not added becase
  38.       no scopes were provided.
  39.     '''
  40.         if not hasattr(token, 'scopes') or not (token.scopes):
  41.             return False
  42.         for scope in token.scopes:
  43.             self._tokens[str(scope)] = token
  44.         
  45.         return True
  46.  
  47.     
  48.     def find_token(self, url):
  49.         '''Selects an Authorization header token which can be used for the URL.
  50.  
  51.     Args:
  52.       url: str or atom.url.Url or a list containing the same.
  53.           The URL which is going to be requested. All
  54.           tokens are examined to see if any scopes begin match the beginning
  55.           of the URL. The first match found is returned.
  56.  
  57.     Returns:
  58.       The token object which should execute the HTTP request. If there was
  59.       no token for the url (the url did not begin with any of the token
  60.       scopes available), then the atom.http_interface.GenericToken will be 
  61.       returned because the GenericToken calls through to the http client
  62.       without adding an Authorization header.
  63.     '''
  64.         if url is None:
  65.             return None
  66.         if isinstance(url, (str, unicode)):
  67.             url = atom.url.parse_url(url)
  68.         
  69.         if url in self._tokens:
  70.             token = self._tokens[url]
  71.             if token.valid_for_scope(url):
  72.                 return token
  73.             del self._tokens[url]
  74.         
  75.         for scope, token in self._tokens.iteritems():
  76.             if token.valid_for_scope(url):
  77.                 return token
  78.         
  79.         return atom.http_interface.GenericToken()
  80.  
  81.     
  82.     def remove_token(self, token):
  83.         '''Removes the token from the token_store.
  84.  
  85.     This method is used when a token is determined to be invalid. If the
  86.     token was found by find_token, but resulted in a 401 or 403 error stating
  87.     that the token was invlid, then the token should be removed to prevent
  88.     future use.
  89.  
  90.     Returns:
  91.       True if a token was found and then removed from the token
  92.       store. False if the token was not in the TokenStore.
  93.     '''
  94.         token_found = False
  95.         scopes_to_delete = []
  96.         for scope, stored_token in self._tokens.iteritems():
  97.             if stored_token == token:
  98.                 scopes_to_delete.append(scope)
  99.                 token_found = True
  100.                 continue
  101.         
  102.         for scope in scopes_to_delete:
  103.             del self._tokens[scope]
  104.         
  105.         return token_found
  106.  
  107.     
  108.     def remove_all_tokens(self):
  109.         self._tokens = { }
  110.  
  111.  
  112.